/*
 * @(#)RawStorer.java	0.1 01/06/05
 *
 * Copyright 2004 Fabio Gasparetti. All rights reserved.
 */

package org.tabularium.text.se;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.tabularium.net.downloader.Resource;
import org.tabularium.text.TermWeightVector;

/**
 * Provides storage for raw copies of objects, e.g., Web pages, Java objects.
 * Keys are strings.
 */
public abstract class RawStorer {
	/**
	 * It doesn't check if the id key is already stored.
	 * 
	 * @param id
	 * @param obj
	 * @throws IOException
	 */
	public abstract void store(String id, byte[] obj) throws IOException;

	public abstract void store(String id, byte[] obj, long timestamp) throws IOException;
	
	/**
	 * It doesn't check if the resource key is already stored with a different value type.
	 * 
	 * @param res
	 * @throws IOException
	 */
	public abstract void store(Resource res) throws IOException;

	public abstract List<byte[]> retrieve(String id) throws IOException;

	public abstract byte[] retrieve(String id, long timestamp) throws IOException;

	public abstract List<Resource> retrieveResource(String id) throws IOException;

	public abstract Resource retrieveResource(String id, long timestamp) throws IOException;

	public abstract List<Long> timestamp(String id) throws IOException;

	public abstract void remove(String id) throws IOException;

	public abstract void remove(String id, long timestamp) throws IOException;

	public abstract DocIterator browse() throws IOException;

	public abstract int size() throws IOException;

	public abstract void clear() throws IOException;

	public static byte[] serialize(Object obj) throws IOException {
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		ObjectOutputStream o = new ObjectOutputStream(b);
		o.writeObject(obj);
		byte[] bb = b.toByteArray();
		o.close();
		b.close();
		return bb;
	}

	public static Object deserialize(byte[] bytes) throws IOException,
			ClassNotFoundException {
		ByteArrayInputStream b = new ByteArrayInputStream(bytes);
		ObjectInputStream o = new ObjectInputStream(b);
		Object obj = o.readObject();
		o.close();
		b.close();
		return obj;
	}

//	public static OutputStream serializeToStream(Object obj) throws IOException {
//		ByteArrayOutputStream b = new ByteArrayOutputStream();
//		ObjectOutputStream o = new ObjectOutputStream(b);
//		return o;
//	}
//
//	public static Object deserializeFromStream(InputStream is) throws IOException,
//			ClassNotFoundException {
//		ObjectInputStream o = new ObjectInputStream(is);
//		Object obj = o.readObject();
//		o.close();
//		return obj;
//	}
}
